/**
 *====================================================
 * 文件名称: FinanceProductActionApiImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月11日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.project.action.v1.dto.FinanceProductCastor;
import com.yuexunit.finance.project.action.v1.model.FinanceProduct;
import com.yuexunit.finance.project.action.v1.model.FinanceProductExpense;
import com.yuexunit.finance.project.action.v1.model.WarningScheme;
import com.yuexunit.finance.project.action.v1.model.WarningSchemeResult;
import com.yuexunit.finance.project.action.v1.model.input.FinanceProductUpdate;
import com.yuexunit.finance.project.action.v1.model.input.WarningSchemeUpdateParams;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductExpenseDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.WarningSchemeDetail;
import com.yuexunit.finance.project.action.v1.model.output.WarningSchemeLineInfo;
import com.yuexunit.finance.project.entity.FinanceProductEntity;
import com.yuexunit.finance.project.entity.FinanceProductExpenseEntity;
import com.yuexunit.finance.project.entity.FinanceProductWarningEntity;
import com.yuexunit.finance.project.entity.WarningSchemeEntity;
import com.yuexunit.finance.project.entity.WarningSchemeResultEntity;
import com.yuexunit.finance.project.entity.lexicon.BillingMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.ExpenseMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.ExpenseScopeEnum;
import com.yuexunit.finance.project.entity.lexicon.ExpenseTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.FinanceStateEnum;
import com.yuexunit.finance.project.entity.lexicon.FrequencyEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductScopeEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.RelationTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.SettlementStateEnum;
import com.yuexunit.finance.project.entity.lexicon.TreatmentMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.UnitEnum;
import com.yuexunit.finance.project.entity.lexicon.WarningFactorEnum;
import com.yuexunit.finance.project.entity.lexicon.WarningLevelEnum;
import com.yuexunit.finance.project.entity.lexicon.WarningValueTypeEnum;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceProductExpenseService;
import com.yuexunit.finance.project.service.FinanceProductService;
import com.yuexunit.finance.project.service.FinanceProductWarningService;
import com.yuexunit.finance.project.service.WarningSchemeResultService;
import com.yuexunit.finance.project.service.WarningSchemeService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.persist.hibernate.SessionRepository;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinanceProductActionApiImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月11日 下午6:03:19
 */
@Implements(FinanceProductActionApi.class)
public class FinanceProductActionApiImpl implements FinanceProductActionApi {

	private BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);

	private FinanceProductService financeProductService = ComponentFactory.loadClass(FinanceProductService.class);

	private FinanceProductExpenseService financeProductExpenseService = ComponentFactory.loadClass(FinanceProductExpenseService.class);

	private WarningSchemeResultService warningSchemeResultService = ComponentFactory.loadClass(WarningSchemeResultService.class);

	private WarningSchemeService warningSchemeService = ComponentFactory.loadClass(WarningSchemeService.class);

	private FinanceProductWarningService financeProductWarningService = ComponentFactory.loadClass(FinanceProductWarningService.class);

	private FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);

	private FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);


	private void validateContract(FinanceProductEntity entity) {
		if (ProductScopeEnum.ENTERPRISE == entity.getProductScope()) {
			return;
		}
		List<FinanceProductEntity> list = financeProductService.inquireFinanceProduct(null, entity.getProductScope(), null, ProductStateEnum.PUBLISHED, null, entity.getStartDate(), entity.getEndDate(), PageCriteria.defaultInstance());
		if (!list.isEmpty()) {
			throw new BusinessException("有效期内已存在发布的合同信息");
		}
	}

	@Transactional
	@Override
	public String createFinanceProduct(FinanceProduct params) {
		FinanceProductEntity entity = FinanceProductCastor.fromFinanceProduct(params);
		validateContract(entity);
		financeProductService.createFinanceProduct(entity);
		return entity.getProductId().toString();
	}

	@Transactional
	@Override
	public void updateFinanceProduct(FinanceProductUpdate params) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(params.getProductId());
		if (ProductStateEnum.WAIT_SUBMIT != persist.getState()) {
			throw new BusinessException("产品已提交,不允许修改");
		}
		FinanceProductEntity entity = FinanceProductCastor.fromFinanceProduct(params);
		entity.setProductId(params.getProductId());
		validateContract(entity);
		financeProductService.updateFinanceProduct(entity);
	}

	@Transactional
	@Override
	public void disableFinanceProduct(Long productId) {
		financeProductService.disableFinanceProduct(productId);
	}

	@Transactional
	@Override
	public void submitFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		validateContract(persist);
		persist.setState(ProductStateEnum.WAIT_ACCEPT);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Transactional
	@Override
	public void auditFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		validateContract(persist);
		persist.setState(ProductStateEnum.WAIT_PUBLISH);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Transactional
	@Override
	public void publishFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		persist.setState(ProductStateEnum.PUBLISHED);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Override
	public void topFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		persist.setTop(Boolean.TRUE);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Override
	public void cancelTopFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		persist.setTop(Boolean.FALSE);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Transactional
	@Override
	public void deprecatedFinanceProduct(Long productId) {
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		persist.setState(ProductStateEnum.DEPRECATED);
		persist.setTop(Boolean.FALSE);
		financeProductService.renovateFinanceProduct(persist);
	}

	@Override
	public FinanceProductDetail inquireFinanceProductDetail(Long productId) {
		if (productId == null || productId == 0) {
			return null;
		}
		System.out.println(productId);
		FinanceProductEntity persist = financeProductService.inquireFinanceProduct(productId);
		return FinanceProductCastor.toFinanceProductDetail(persist);
	}

	@Override
	//查询产品列表
	public PageList<FinanceProductLineInfo> inquireFinanceProductPageList(Integer productScope, Integer productState, Integer productType, String productName, String startDate, String endDate, PageCriteria page) {
		Long enterpriseId = null;
		if (FinanceSessionContext.isEnterprise()) {
			enterpriseId = FinanceSessionContext.getEnterpriseId();
		}
		ProductScopeEnum scope = null;
		if (productScope != null) {
			scope = ProductScopeEnum.valueOf(productScope);
		}
		ProductStateEnum state = null;
		if (productState != null) {
			state = ProductStateEnum.valueOf(productState);
		}
		ProductTypeEnum type = null;
		if (productType != null) {
			type = ProductTypeEnum.valueOf(productType);
		}

		PageList<FinanceProductEntity> pageList = financeProductService.inquireFinanceProduct(enterpriseId, scope, type, state, productName, DateUtils.parseDateInt(startDate), DateUtils.parseDateInt(endDate), page);
		PageList<FinanceProductLineInfo> results = new PageList<>(page, pageList.getTotalRecordCount());
		for (FinanceProductEntity fpe : pageList) {
			results.add(FinanceProductCastor.toFinanceProductLineInfo(fpe));
		}
		return results;
	}

	@Transactional
	@Override
	public String createFinanceProductExpense(FinanceProductExpense params) {
		if (params.getStartDay() != null && params.getEndDay() != null) {
			if (params.getStartDay() > params.getEndDay()) {
				int temp = params.getStartDay();
				params.setStartDay(params.getEndDay());
				params.setEndDay(temp);
			}
		} else {
			params.setStartDay(null);
			params.setEndDay(null);
		}
		FinanceProductExpenseEntity entity = new FinanceProductExpenseEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setExpenseType(ExpenseTypeEnum.valueOf(params.getExpenseType()));
		entity.setExpenseMethod(ExpenseMethodEnum.valueOf(params.getExpenseMethod()));
		entity.setUnit(UnitEnum.valueOf(params.getUnit()));
		entity.setBillingMethod(BillingMethodEnum.valueOf(params.getBillingMethod()));
		entity.setExpenseScope(ExpenseScopeEnum.valueOf(params.getExpenseScope()));
		entity.setProportion(BooleanUtils.toBoolean(params.getProportion()));
		if (ExpenseTypeEnum.AHEAD == entity.getExpenseType() || ExpenseTypeEnum.DELAY == entity.getExpenseType()) {
			entity.setBillingMethod(BillingMethodEnum.DAYS);
			entity.setProportion(Boolean.TRUE);
			if (entity.getBulkstockInfoId() != null) {
				BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(entity.getBulkstockInfoId());
				entity.setBulkstockCategoryId(bulkstockInfo.getBulkstockCategoryId());
				entity.setCategoryName(bulkstockInfo.getCategoryName());
				entity.setBulkstockName(bulkstockInfo.getBulkstockName());
			} else {
				entity.setBulkstockName(null);
			}
			if (entity.getBulkstockCategoryId() == null) {
				entity.setCategoryName(null);
				entity.setBulkstockInfoId(null);
				entity.setBulkstockName(null);
			}
			if (entity.getBulkstockInfoId() == null && entity.getBulkstockCategoryId() == null) {
				entity.setExpenseMethod(ExpenseMethodEnum.OTHER);
			} else {
				entity.setExpenseMethod(ExpenseMethodEnum.BULKSTOCK);
			}
		}
		if (ExpenseMethodEnum.BULKSTOCK == entity.getExpenseMethod()) {
			if (entity.getBulkstockCategoryId() == null || entity.getBulkstockInfoId() == null) {
				throw new BusinessException("按货方式,货物信息不允许为空");
			}
		} else if (ExpenseMethodEnum.NATURE == entity.getExpenseMethod()) {
			entity.setBulkstockCategoryId(null);
			entity.setCategoryName(null);
			entity.setBulkstockInfoId(null);
			entity.setBulkstockName(null);
			entity.setModel(null);
			entity.setDimension(null);
		} else if (ExpenseMethodEnum.STANDARD == entity.getExpenseMethod()) {
			entity.setBulkstockCategoryId(null);
			entity.setCategoryName(null);
			entity.setBulkstockInfoId(null);
			entity.setBulkstockName(null);
		} else {
			entity.setBulkstockCategoryId(null);
			entity.setCategoryName(null);
			entity.setBulkstockInfoId(null);
			entity.setBulkstockName(null);
			entity.setModel(null);
			entity.setDimension(null);
		}
		if (ExpenseTypeEnum.AHEAD == entity.getExpenseType()) {
			entity.setExpenseScope(ExpenseScopeEnum.NORMAL);
		} else if (ExpenseTypeEnum.DELAY == entity.getExpenseType()) {
			entity.setExpenseScope(ExpenseScopeEnum.OVERDUE);
		}
		financeProductExpenseService.createFinanceProductExpense(entity);
		return entity.getProductExpenseId().toString();
	}

	@Transactional
	@Override
	public void deleteFinanceProductExpense(Long productExpenseId) {
		financeProductExpenseService.deleteFinanceProductExpense(productExpenseId);
	}

	@Override
	public PageList<FinanceProductExpenseDetail> inquireFinanceProductExpensePageList(Long productId, String expenseName, PageCriteria page) {
		PageList<FinanceProductExpenseEntity> pageList = financeProductExpenseService.inquireFinanceProductExpensePageList(productId, ExpenseTypeEnum.EXPENSE, expenseName, page);
		PageList<FinanceProductExpenseDetail> results = new PageList<>(page, pageList.getTotalRecordCount());
		for (FinanceProductExpenseEntity fpee : pageList) {
			results.add(FinanceProductCastor.toFinanceProductExpense(fpee));
		}
		return results;
	}

	@Transactional
	@Override
	public String createWarningScheme(WarningScheme params) {
		WarningSchemeEntity entity = new WarningSchemeEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setFactor(WarningFactorEnum.valueOf(params.getFactor()));
		entity.setValueType(WarningValueTypeEnum.valueOf(params.getValueType()));
		entity.setRelationType(RelationTypeEnum.valueOf(params.getRelationType()));
		entity.setTreatmentMethod(TreatmentMethodEnum.valueOf(params.getTreatmentMethod()));
		entity.setFrequency(FrequencyEnum.valueOf(params.getFrequency()));
		Long schemeId = warningSchemeService.createWarningScheme(entity);
		return schemeId.toString();
	}

	@Transactional
	@Override
	public void updateWarningScheme(WarningSchemeUpdateParams params) {
		WarningSchemeEntity entity = new WarningSchemeEntity();
		entity.setFactor(WarningFactorEnum.valueOf(params.getFactor()));
		entity.setValueType(WarningValueTypeEnum.valueOf(params.getValueType()));
		entity.setRelationType(RelationTypeEnum.valueOf(params.getRelationType()));
		entity.setTreatmentMethod(TreatmentMethodEnum.valueOf(params.getTreatmentMethod()));
		entity.setFrequency(FrequencyEnum.valueOf(params.getFrequency()));
		warningSchemeService.updateWarningScheme(entity);
	}

	@Transactional
	@Override
	public void removeWarningScheme(Long schemeId) {
		warningSchemeService.deleteWarningScheme(schemeId);
	}

	@Override
	@Transactional
	public void removeProductWarningScheme(Long productId, Long schemeId) {
		warningSchemeService.deleteProductWarningScheme(productId, schemeId);
	}

	@Override
	public WarningSchemeDetail inquireWarningSchemeDetail(Long schemeId) {
		WarningSchemeEntity entity = warningSchemeService.inquireWarningScheme(schemeId);
		return FinanceProductCastor.toWarningSchemeDetail(entity);
	}

	@Override
	public PageList<WarningSchemeLineInfo> inquireWarningSchemePageList(Integer _factor, String _startDate, String _endDate, PageCriteria page) {
		WarningFactorEnum factor = null;
		Date startDate = null;
		Date endDate = null;
		if (_factor != null) {
			factor = WarningFactorEnum.valueOf(_factor);
		}
		if (StringUtils.isNotBlank(_startDate)) {
			startDate = DateUtil.from(_startDate);
		}
		if (StringUtils.isNotBlank(_endDate)) {
			startDate = DateUtil.from(_endDate);
		}
		PageList<WarningSchemeEntity> pages = warningSchemeService.inquireWarningSchemePageList(factor, startDate, endDate, page);
		PageList<WarningSchemeLineInfo> results = new PageList<>(page, pages.getTotalRecordCount());
		for (WarningSchemeEntity entity : pages) {
			results.add(FinanceProductCastor.toWarningScheme(entity));
		}
		return results;
	}

	@Transactional
	@Override
	public void updateProductWarningScheme(Long productId, Long[] schemeIds) {
		List<FinanceProductWarningEntity> list = financeProductWarningService.inquireFinanceProductWarningList(productId);
		for (Long schemeId : schemeIds) {
			boolean exists = false;
			for (FinanceProductWarningEntity p : list) {
				if (p.getSchemeId().equals(schemeId)) {
					list.remove(p);
					exists = true;
					break;
				}
			}
			if (exists) {
				continue;
			}
			FinanceProductWarningEntity entity = new FinanceProductWarningEntity();
			entity.setProductId(productId);
			entity.setSchemeId(schemeId);
			financeProductWarningService.createFinanceProductWarning(entity);
		}
		for (FinanceProductWarningEntity p : list) {
			financeProductWarningService.deleteFinanceProductWarning(p.getProductWarningId());
		}
	}

	@Override
	public PageList<WarningSchemeResult> inquireWarningSchemeResultPageList(Integer _factor, String _startDate, String _endDate, Integer settlementState, String financeContractSerialNumber, String financeNumber, String enterpriseName, Integer productType, PageCriteria page) {
		PageList<WarningSchemeResult> wsrs = null;
		String sql = "SELECT count(*) c FROM view_fin_scheme_result v WHERE "
				+ " (v.FACTOR = "+_factor+" OR "+(_factor == null)+") "
				+ " AND (v.FINANCE_CONTRACT_SERIAL_NUMBER LIKE '%"+financeContractSerialNumber+"%' OR "+(StringUtils.isBlank(financeContractSerialNumber))+") "
				+ " AND (v.FINANCE_NUMBER LIKE '%"+financeNumber+"%' OR "+ (StringUtils.isBlank(financeNumber)) +") "
				+ " AND (v.SETTLEMENT_STATE = "+settlementState+" OR "+(settlementState == null)+")"
				+ " AND (v.ENTERPRISE_NAME LIKE '%"+enterpriseName+"%' OR "+(StringUtils.isBlank(enterpriseName))+")"
				+ " AND (v.PRODUCT_TYPE = "+productType+" OR "+(productType == null)+")";
		Session session = SessionRepository.getCurrentSession();
		List<Object> datas = session.createSQLQuery(sql).addScalar("c").list();
		for (Object l : datas) {
			Object os = (Object)l;
			wsrs = new PageList<>(page, ((BigInteger) os).intValue());
		}

		session = SessionRepository.getCurrentSession();
		//		String sql = "SELECT FINANCE_ID FROM fin_loan_notice c WHERE FINANCE_ID IN (SELECT FINANCE_ID FROM finance_files_info b,SIGN_FILE_INFO a WHERE b.FILES_UUID LIKE a.FILE_UUID AND a.FILE_SIGN_STATE = 6 AND a.FILES_SIGN = 1 GROUP BY FINANCE_ID) AND c.LEADER2_AUDIT_STATE = 1 AND c.LEADER2_AUDIT_SUBMIT_STATE = 1";
		sql = "SELECT * FROM view_fin_scheme_result v WHERE "
				+ " (v.FACTOR = "+_factor+" OR "+(_factor == null)+") "
				+ " AND (v.FINANCE_CONTRACT_SERIAL_NUMBER LIKE '%"+financeContractSerialNumber+"%' OR "+(StringUtils.isBlank(financeContractSerialNumber))+") "
				+ " AND (v.FINANCE_NUMBER LIKE '%"+financeNumber+"%' OR "+ (StringUtils.isBlank(financeNumber)) +") "
				+ " AND (v.SETTLEMENT_STATE = "+settlementState+" OR "+(settlementState == null)+")"
				+ " AND (v.ENTERPRISE_NAME LIKE '%"+enterpriseName+"%' OR "+(StringUtils.isBlank(enterpriseName))+")"
				+ " AND (v.PRODUCT_TYPE = "+productType+" OR "+(productType == null)+")"
				
				+ " ORDER BY DATE_FORMAT(v.CREATE_DT,'%Y%m%d') DESC, CONVERT(v.ENTERPRISE_NAME USING gbk ) COLLATE gbk_chinese_ci ASC "	
				+ " LIMIT " + (page.getPageIndex() - 1)*page.getPageSize() + " , " + page.getPageSize();
		
		
		datas = session.createSQLQuery(sql)
				.addScalar("SCHEME_RESULT_ID")
				.addScalar("SETTLEMENT_STATE")
				.addScalar("FINANCE_ID")
				.addScalar("SCHEME_NAME")
				.addScalar("FACTOR")
				.addScalar("WARNING_LEVEL")
				.addScalar("ENTERPRISE_NAME")
				.addScalar("FINANCE_NUMBER")
				.addScalar("FINANCE_CONTRACT_SERIAL_NUMBER")
				.addScalar("PRODUCT_TYPE")
				.addScalar("BULKSTOCK_NAME")
				.addScalar("SPECIFICATION_NAME")
				.addScalar("WEIGHT")
				.addScalar("VALUATION_QUANTITY")
				.addScalar("EVALUATE_PRICE")
				.addScalar("LATEST_PRICE")
				.addScalar("TOTAL_LOAN_AMOUNT")
				.addScalar("INCREASE")
				.addScalar("RISE_FALL")
				.addScalar("FINANCE_STATE")
				.addScalar("CREATE_DT")
				.addScalar("SETTLEMENT_START")
				.addScalar("SETTLEMENT_END")
				.addScalar("FINANCE_DAYS")
				.addScalar("INTEREST_RATE")
				.addScalar("DUE_DAYS")
				.addScalar("PLEDGE_RATE")
				.list();
		for (Object l : datas) {
			Object[] os = (Object[])l;
			WarningSchemeResult wsr = new WarningSchemeResult();
			wsr.setSchemeResultId(((BigInteger) os[0]).longValue());
			wsr.setFinanceId(((BigInteger) os[2]).longValue());
			wsr.setSchemeName((String) os[3]);
			wsr.setFactor(((Byte) os[4]).intValue());
			wsr.setFactorView((WarningFactorEnum.valueOf(wsr.getFactor())).getValue());
			if (os[5] != null) {
				wsr.setWarningLevel(Integer.parseInt((String) os[5]));
			}
			wsr.setWarningLevelView((WarningLevelEnum.valueOf(wsr.getWarningLevel())).getValue());
			wsr.setEnterpriseName((String) os[6]);
			wsr.setFinanceNumber((String) os[7]);
			wsr.setFinanceContractSerialNumber((String) os[8]);
			wsr.setProductType(((Byte) os[9]).intValue());
			wsr.setProductTypeView((ProductTypeEnum.valueOf(wsr.getProductType())).getValue());
			wsr.setBulkstockName((String) os[10]);
			wsr.setSpecification((String) os[11]);
			wsr.setWeight((BigDecimal) os[12]);
			wsr.setPledgeRate((BigDecimal) os[26]);
			wsr.setEvaluatePrice((BigDecimal) os[14]);
			wsr.setLatestPrice((BigDecimal) os[15]);
			wsr.setLatestValuation(wsr.getLatestPrice().multiply(wsr.getWeight()));
			wsr.setIncrease((BigDecimal) os[17]);
			wsr.setRiseFall((BigDecimal) os[18]);
			wsr.setFinanceState(((Byte) os[19]).intValue());
			wsr.setFinanceStateView((FinanceStateEnum.valueOf(wsr.getFinanceState())).getValue());
			wsr.setCreateDateTime((Date) os[20]);
			wsr.setLoanAmount((BigDecimal) os[16]);
			
			wsr.setSettlementStart((Date) os[21]);
			wsr.setSettlementEnd((Date) os[22]);
			wsr.setFinanceDays((Integer) os[23]);
			wsr.setInterestRate((BigDecimal) os[24]);
			wsr.setDueDays((Integer) os[25]);
			wsrs.add(wsr);
		}
		return wsrs;
	}

	@Override
	@Transactional
	public void settlementSchemeResult(Long schemeResultId, Integer settlementState, String settlementMeasure, String settlementDetail, String operator,
			Date operatorTime) {
		WarningSchemeResultEntity entity = warningSchemeResultService.inquireWarningSchemeResult(schemeResultId);
		entity.setSettlementState(SettlementStateEnum.valueOf(settlementState));
		entity.setSettlementMeasure(settlementMeasure);
		entity.setSettlementDetail(settlementDetail);
		entity.setOperator(operator);
		entity.setOperatorTime(operatorTime);
		warningSchemeResultService.updateSchemeResult(entity);
	}

	@Override
	public WarningSchemeResult inquireWarningSchemeResultDetail(Long schemeResultId) {
		WarningSchemeResultEntity wsre = warningSchemeResultService.inquireWarningSchemeResult(schemeResultId);
		WarningSchemeResult wsr = new WarningSchemeResult();
		BeanUtils.copyProperties(wsre, wsr);
		wsr.setFactor(wsre.getFactor().ordinal());
		wsr.setWarningLevel(wsre.getWarningLevel().ordinal());

		return wsr;
	}
}
